<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\activity\score;

use app\dao\activity\score\StoreScoreRuleDao;
use app\model\product\product\StoreProduct;
use app\model\product\sku\StoreProductAttrValue;
use app\model\product\sku\StoreProductStockRelation;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;

/**
 * Class StoreScoreRuleServices
 * @package app\services\activity\score
 */
class StoreScoreRuleServices extends BaseServices
{
    /**
     * StoreScoreRuleServices constructor.
     * @param StoreScoreRuleDao $dao
     */
    public function __construct(StoreScoreRuleDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where, int $page = 0, int $limit = 0)
    {
        if (!$page && !$limit) {
            [$page, $limit] = $this->getPageValue();
        }

        $list = $this->dao->getList($where, $page, $limit);
        $count = $this->dao->getListCount($where);
        foreach ($list as $key => $val){
            unset($list[$key]['product_ids']);
            unset($list[$key]['products']);
        }
        return compact('list', 'count');
    }

    /**
     * 新增编辑
     * @param array $data
     * @return mixed
     */
    public function save(array $data)
    {
        $id = $data['id'];
        $data['products'] = json_encode($data['products']);
        unset($data['id']);
        $info = $this->transaction(function () use ($id, $data) {
            if ($id) {
                $data['add_time'] = time();
                $info = $this->dao->update($id, $data);
            } else {
                unset($data['id']);
                $data['add_time'] = time();
                $info = $this->dao->save($data);
            }
            return $info;
        });
        return $info;
    }

    /**
     * 删除
     * @param int $id
     */
    public function del(int $id)
    {
        $this->transaction(function () use ($id) {
            $this->dao->delete($id);
        });
    }

    /**
     * 获取详情
     * @param $id
     * @return array
     */
    public function read(int $id)
    {
        $info = $this->dao->read($id);
        $products = [];
        if($info['product_type'] == 0){
            $pros = json_decode($info['products']);
            foreach ($pros as $val){
                $product = StoreProduct::where('id',$val->product_id)->find();
                $attrValue = StoreProductAttrValue::where('unique', $val->product_attr_value_unique)->find();

                $products[] = [
                    'product_id' => $val->product_id,
                    'image' =>  $attrValue ? $attrValue['image'] : '',
                    'store_name' => $product ? $product->store_name : '',
                    'suk'   =>  $attrValue ? $attrValue->suk : '',
                    'bar_code'  =>  $attrValue ? $attrValue->bar_code : '',
                    'price' =>  $attrValue ? $attrValue->price : '',
                    'stock' =>  $attrValue ? $attrValue->stock : 0,
                    'product_attr_value_unique' => $val->product_attr_value_unique,
                ];
            }
        }
        $info['products'] = $products;
        return $info;
    }
}
